using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using CodePlex.Resourceful.Bookmarks.Model;
using CodePlex.Resourceful.Bookmarks;

namespace CodePlex.Resourceful.Examples
{
    public static class BookmarkServiceExample
    {


        public static void Run()
        {
            string address = Dns.GetHostEntry(Dns.GetHostName())
            .AddressList[0]
            .ToString();


            // test 1 (http-listener-based)

            Uri rootUri = new Uri(string.Format("http://localhost:3000/v1/", address));

            BookmarkService service = new BookmarkService();

            HttpServer host = new HttpServer(rootUri);

            host.ReceiveWebRequest += delegate(HttpListenerContext context)
            {
                service.Process(new HttpListenerContextAdapter(context, rootUri));
            };



            host.Start();

            TestClient(rootUri);

            host.Stop();



            // test 2  (http-handler-based)
            rootUri = new Uri("http://localhost:43908/v1/");

            TestClient(rootUri);
        }




        private static void TestClient(Uri rootUri)
        {
            Console.WriteLine("connecting to: " + rootUri);






            // use the bookmarks client

            Client client = new Client(rootUri);


            // trace all requests/responses to std out
            client.Tracer.AddListener(Console.Out);




            UserAccount user;
            ClientResponse r;

            // view the user
            r = client.GetRepresentation("users/leonardr", MediaType.ApplicationXml);

            // not found, so create the user
            if (r.HttpStatus == HttpStatus.NotFound)
            {

                user = new UserAccount();
                user.Name = "leonardr";
                user.FullName = "Leonard Richardson";
                user.Email = "leonardr@example.com";
                user.Password = "mypassword";


                r = client.PostRepresentation("users", MediaType.ApplicationXml, Utils.ToString(user.ToXml));

            }


            r = client.GetRepresentation("users/leonardr", MediaType.ApplicationXml);

            user = UserAccount.FromXml(r.AsTextReader());

            Console.WriteLine(user);

            // update user profile (auth first)
            client.SetBasicAuth("users/leonardr", user.Name, user.Password);

            user.Password = "opensesame";
            r = client.PutRepresentation("users/leonardr", MediaType.ApplicationXml, Utils.ToString(user.ToXml));

            // check updated...
            r = client.GetRepresentation("users/leonardr", MediaType.ApplicationExWwwFormUrlencoded);
   


            // now delete (re-auth first)
            client.SetBasicAuth("users/leonardr", user.Name, user.Password);
            r = client.DeleteRepresentation("users/leonardr");
            // ensure...
            r = client.GetRepresentation("users/leonardr", MediaType.ApplicationXml);



            // re-add user
            r = client.PostRepresentation("users", MediaType.ApplicationXml, Utils.ToString(user.ToXml));

            // create bookmark
            Bookmark newBookmark = new Bookmark();
            newBookmark.URI = "http://bitworking.org/news/125/REST-and-WS";
            newBookmark.ShortDescription = "REST and WS-*";
            newBookmark.LongDescription = "Joe Gregorio's lucid explanation of RESTful principles";
            newBookmark.IsPublic = true;

            newBookmark.UserID = user.Name;

            r = client.PostRepresentation("users/leonardr/bookmarks", MediaType.ApplicationXml, Utils.ToString(newBookmark.ToXml));


            // update (not the uri)
            newBookmark.ShortDescription = "REST and WS-Death-*";
            string bookmarkUri = "users/leonardr/bookmarks/" + newBookmark.URIHash;
            r = client.PutRepresentation(bookmarkUri, MediaType.ApplicationXml, Utils.ToString(newBookmark.ToXml));

            // update (the uri)
            newBookmark.URI = "http://www.bitworking.org/news/125/REST-and-WS";
            r = client.PutRepresentation(bookmarkUri, MediaType.ApplicationXml, Utils.ToString(newBookmark.ToXml));
            string location = r.Headers[HttpHeader.Location];
            bookmarkUri = "users/leonardr/bookmarks/" + newBookmark.URIHash;

            // get the bookmark
            newBookmark = Bookmark.FromXml(client.GetRepresentation(bookmarkUri, MediaType.ApplicationXml).AsTextReader());

            // modified?
            r = client.GetRepresentation(bookmarkUri, MediaType.ApplicationXml, newBookmark.Timestamp);


            // get all bookmarks
            client.GetRepresentation("users/leonardr/bookmarks.atom");



            // delete bookmark
            client.SetBasicAuth("users/leonardr", user.Name, user.Password);
            r = client.DeleteRepresentation(bookmarkUri);









            Utils.Prompt();

        }


    }

}
